﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

namespace MiniFox.Utility
{
    /// <summary>
    /// 
    /// </summary>
    public static class JsonUtility
    {
        /// <summary>
        /// 
        /// </summary>
        public const string JSON_NODE_FORMAT = "\"{0}\":\"{1}\"";
        /// <summary>
        /// 
        /// </summary>
        static JsonUtility()
        {
        }

        private static string NameValueCollectionToJsonString(NameValueCollection nv)
        {
            if (nv != null && nv.Count > 0)
            {
                List<string> strlist = new List<string>();
                foreach (string key in nv.Keys)
                {
                    string val = nv[key];
                    strlist.Add(string.Format(JSON_NODE_FORMAT, key, val));
                }
                return "{" + string.Join(",", strlist.ToArray()) + "}";
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJsonString<T>(this T obj)
        {
            if (obj is NameValueCollection)
            {
                var nv = obj as NameValueCollection;
                return NameValueCollectionToJsonString(nv);
            }
            return JsonConvert.SerializeObject(obj);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static string ToJsonString<T>(this T obj, JsonSerializerSettings settings)
        {
            if (obj is NameValueCollection)
            {
                var nv = obj as NameValueCollection;
                return NameValueCollectionToJsonString(nv);
            }
            return JsonConvert.SerializeObject(obj, settings);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJsonString(this object obj)
        {
            return ToJsonString<object>(obj);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static string ToJsonString(this object obj, JsonSerializerSettings settings)
        {
            return ToJsonString<object>(obj, settings);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T ToJsonObject<T>(this string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static T ToJsonObject<T>(this string json, JsonSerializerSettings settings)
        {
            return JsonConvert.DeserializeObject<T>(json, settings);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static object ToJsonObject(this string json)
        {
            return JsonConvert.DeserializeObject(json);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static object ToJsonObject(this string json, JsonSerializerSettings settings)
        {
            return JsonConvert.DeserializeObject(json, settings);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object ToJsonObject(this string json, Type type)
        {
            return JsonConvert.DeserializeObject(json, type);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <param name="type"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static object ToJsonObject(this string json, Type type, JsonSerializerSettings settings)
        {
            return JsonConvert.DeserializeObject(json, type, settings);
        }
    }
}
